package messagebeans;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = {
	      @ActivationConfigProperty(propertyName = "destinationType",
	                                propertyValue = "javax.jms.Queue"),
	      @ActivationConfigProperty(propertyName = "destination",
	                                propertyValue = "jms/RequestQueue") })
	public class ReplierBean implements MessageListener{
	   @Resource(name = "jms/ConnectionFactory")
	   private ConnectionFactory connectionFactory;

	   private static final String INVALID_QUEUE_NAME = "jms/InvalidQueue";
	   

	   public void onMessage(Message message)
	   {
	      try
	      {
	         // Get JMS connection and session
	         Connection connection = connectionFactory.createConnection();
	         Session session = connection.createSession(true, 0);

	         // Check validity of message
	         if ((message instanceof TextMessage)
	               && (message.getJMSReplyTo() != null))
	         {
	            TextMessage requestMessage = (TextMessage) message;

	            System.out.println("Received request");
	            System.out.println("\tTime:       " + System.currentTimeMillis()
	                  + " ms");
	            System.out.println("\tMessage ID: "
	                  + requestMessage.getJMSMessageID());
	            System.out.println("\tCorrel. ID: "
	                  + requestMessage.getJMSCorrelationID());
	            System.out.println("\tReply to:   "
	                  + requestMessage.getJMSReplyTo());
	            System.out.println("\tContents:   " + requestMessage.getText());

	            String contents = "Message has been received successfully!!";
	            Destination replyDestination = message.getJMSReplyTo();
	            MessageProducer replyProducer = session
	                  .createProducer(replyDestination);
	            TextMessage replyMessage = session.createTextMessage();
	            replyMessage.setText(contents);
	            replyMessage.setJMSCorrelationID(requestMessage.getJMSMessageID());
	            replyProducer.send(replyMessage);

	            System.out.println("Sent reply");
	            System.out.println("\tTime:       " + System.currentTimeMillis()
	                  + " ms");
	            System.out.println("\tMessage ID: "
	                  + replyMessage.getJMSMessageID());
	            System.out.println("\tCorrel. ID: "
	                  + replyMessage.getJMSCorrelationID());
	            System.out.println("\tReply to:   " + replyMessage.getJMSReplyTo());
	            System.out.println("\tContents:   " + replyMessage.getText());
	         } else
	         {
	            System.out.println("Invalid message detected");
	            System.out.println("\tType:       " + message.getClass().getName());
	            System.out.println("\tTime:       " + System.currentTimeMillis()
	                  + " ms");
	            System.out.println("\tMessage ID: " + message.getJMSMessageID());
	            System.out
	                  .println("\tCorrel. ID: " + message.getJMSCorrelationID());
	            System.out.println("\tReply to:   " + message.getJMSReplyTo());

	            message.setJMSCorrelationID(message.getJMSMessageID());

	            Destination invalidDestination = session
	                  .createQueue(INVALID_QUEUE_NAME);
	            MessageProducer invalidProducer = session
	                  .createProducer(invalidDestination);
	            TextMessage textMessage = session.createTextMessage();
	            textMessage.setText("Invalid message received");
	            invalidProducer.send(textMessage);

	            System.out.println("Sent to invalid message queue");
	            System.out.println("\tType:       " + message.getClass().getName());
	            System.out.println("\tTime:       " + System.currentTimeMillis()
	                  + " ms");
	            System.out.println("\tMessage ID: " + message.getJMSMessageID());
	            System.out
	                  .println("\tCorrel. ID: " + message.getJMSCorrelationID());
	            System.out.println("\tReply to:   " + message.getJMSReplyTo());
	         }
	         session.close();
	         connection.close();
	      } catch (Exception e)
	      {
	         e.printStackTrace();
	      }
	   }
	}
